<?php
namespace Common\Model;

class Page {
    protected $start;   //起始记录位置
    protected $size   =   20;   //每页记录数
    protected $sideSize =   5; //显示的数字分页导航数量
    protected $total;    //总记录数    
    protected $totalPage;   //总页数
    protected $nowPage;     //当前页数
    protected $param    =   ''; //附加在跳转地址上的参数
    protected $pKey     =   'p'; //传递分页数的变量名
    protected $url = '';
    protected $urlPageTag = '[%PAGE%]';
	protected $style;

    /**
     * .page样式在App/Common/View/default/css/public.css中
     */
    protected $theme = '<div class="page">[FIRST_PAGE][PREV_PAGE][NUM_PAGE][NEXT_PAGE][LAST_PAGE]</div>';

    public function __construct($total, $size = 20, $sideSize = 5, $param = '') {
        $this->total = $total;
        $this->size  = $size;
        $this->param = empty($param)  ? '' : trim($param, '&');
        //$this->pKey  = C('VAR_PAGE');

        $this->totalPage = ceil($this->total / $this->size);
        $nowPage         = empty($_GET[$this->pKey])           ? 1                : (int)$_GET[$this->pKey];
        $this->nowPage   = empty($nowPage) || $nowPage < 1     ? 1                : $nowPage;
        $this->nowPage   = $this->nowPage > $this->totalPage   ? $this->totalPage : $this->nowPage;
        $this->start     = $this->size * ($this->nowPage - 1);

        $this->url = $this->getUrl();
    }

    public function getUrl() {
        $url = $_SERVER['REQUEST_URI'];

        if(!empty($this->param)) {
            $url = $url .(strpos($_SERVER['REQUEST_URI'], '?')  ?   '&'  :   '?') . $this->param;
        }

        $parse = parse_url($url);
        $param = array();

        if(isset($parse['query'])) {
            parse_str($parse['query'],$param);
        }

        $param[$this->pKey] = '[%PAGE%]';
        $url = empty($param)
               ?   $parse['path']
               :   $parse['path']. '?'. http_build_query($param);

        return $url;
    }

    public function url($num) {
        return str_replace(urlencode($this->urlPageTag), $num, $this->url);
    }

    public function fetch() {
        $r = array(
            'start'     => $this->start,
            'p'         => $this->nowPage,
            'size'      => $this->size,
            'total'     => $this->total,
            'totalPage' => $this->totalPage,
            'link'      => '',
        );

        if($this->nowPage == 1) {
            $firstPage = '<span class="disabled"> |< </span>';
            $prevPage  = '<span class="disabled"> << </span>';
        } else {
            $firstPage = '<a href="' . $this->url(1)                  . '"> |< </a>';
            $prevPage  = '<a href="' . $this->url($this->nowPage - 1) . '"> << </a>';
        }

        if($this->totalPage == $this->nowPage) {
            $nextPage = '<span class="disabled"> >> </span>';
            $lastPage = '<span class="disabled"> >| </span>';
        } else {
            $nextPage = '<a href="' . $this->url($this->nowPage + 1) . '"> >> </a>';
            $lastPage = '<a href="' . $this->url($this->totalPage)   . '"> >| </a>';
        }

        $count   = $this->sideSize * 2;
        $numPage = '';

        for($i = 1; $i <= $count; $i++) {
            if($this->nowPage - $this->sideSize <= 0) {
                $tmpPage = $i;
            } elseif ($this->nowPage + $count > $this->totalPage) {
                $tmpPage = $this->totalPage - $count + $i;
            } else {
                $tmpPage = $this->nowPage - $this->sideSize + $i;
            }

            if($this->nowPage == $tmpPage) {
                $numPage .= '<span>' . $tmpPage . '</span></li>';
            } else {
                $numPage .= '<a href="' . $this->url($tmpPage) . '">' . $tmpPage . '</a>';
            }

            if($i >= $this->totalPage) {
                break;
            }
        }

        $r['link'] = str_replace(
            array('[FIRST_PAGE]', '[PREV_PAGE]', '[NUM_PAGE]', '[NEXT_PAGE]', '[LAST_PAGE]'),
            array($firstPage, $prevPage, $numPage, $nextPage, $lastPage),
            $this->theme
        );

        return $r;
    }
}
